package com.lxw.mvpframe.util

import android.graphics.Bitmap
import android.graphics.BitmapFactory

/**
 * 猿代码： Lxw
 * Email: China2021@126.com
 * 时间轴： 2018年 03月 09 日 下午4:46
 */
object BitmapUtil {

    /**
     * [filePath]:文件路径
     * [showWidth]:要显示的宽度
     * [showHeight]:要显示的高度
     * 加载Bitmap
     */
    fun loadBitmap(filePath: String, showWidth: Int, showHeight: Int): Bitmap {
        val option = BitmapFactory.Options()
        option.inJustDecodeBounds = true
        option.inPreferredConfig = Bitmap.Config.RGB_565

        BitmapFactory.decodeFile(filePath, option)

        val outWith = option.outWidth
        val outHeight = option.outHeight

        option.inSampleSize = getSimpleSize(outWith, outHeight, showWidth, showHeight)

        option.inJustDecodeBounds = false
        return BitmapFactory.decodeFile(filePath, option)
    }


    /**
     * bitmap的采样率压缩
     */
    private fun getSimpleSize(outWidth: Int, outHeight: Int, showWidth: Int, showHeight: Int): Int {
        var inSimpleSize = 1
        if (outWidth > outHeight && outWidth > showWidth) {
            inSimpleSize = outWidth / showWidth
        } else if (outWidth < outHeight && outHeight > showHeight) {
            inSimpleSize = outHeight / showHeight
        }
        if (inSimpleSize <= 0) inSimpleSize = 1
        return inSimpleSize
    }
}